package com.yya.ratelimiter.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

/**
 * @Autor: yuanyongan
 * @Description: 通过Spring框架的AOP创建对象
 * @Date 2023/1/13 13:51
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringContextUtils.applicationContext == null){
            SpringContextUtils.applicationContext = applicationContext;
        }
    }

    public static <T> T getBean(Class<T> beanType){return applicationContext.getBean(beanType);}

    public static <T> T getBean(String beanName, Class<T> beanType){return applicationContext.getBean(beanName, beanType);}

    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static Resource getResource(String location){
        return applicationContext.getResource(location);
    }

}
